﻿/******************************************************************
 * 功能描述：缓存服务使用入口，本类提供该项目内所有缓存服务入口
 * 创建时间：2013-06-18
 * 作者：Panxinming
 * 版本：1.0
 * 相关配置信息请参阅：ThinkDev.Caching.config
 * 修订描述：
 * 最后修订日期：
 ******************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ThinkDev.Caching;
using ThinkDev.Caching.Caches;

namespace EMoney.IStock.OAuth.Framework
{
    public class CacheManager
    {
        public static IRedis RedisCache;
        public static ICache RuntimeCache;
        public static ICache DefaultCache;

        static CacheManager()
        {
            RedisCache = (RedisCache)(CacheFactory.GetCache("RedisDefault"));
            RuntimeCache = CacheFactory.GetCache("runtime");
            DefaultCache = CacheFactory.GetCache("RedisDefault");
        }
    }

    /// <summary>
    /// 金融平台 Redis缓存
    /// </summary>
    public class JRPTCommCache
    {
        static string GetKey(string key)
        {
            key = MD5Hash.GetMD5Key(key);
            return "EMoney:JRPTIStock:" + ConfigHelper.JRPTCachePre + key;
        }

        #region Redis缓存

        /// <summary>
        /// 获取Redis缓存
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="key">key</param>
        /// <returns></returns>
        public static T GetCache<T>(string key)
        {
            try
            {
                key = GetKey(key);
                return CacheManager.DefaultCache.Get<T>(key);
            }
            catch
            {
                return default(T);
            }
        }

        /// <summary>
        /// 添加Redis缓存
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="key">key</param>
        /// <param name="t">数据</param>
        /// <param name="minutes">时间(分)0永久</param>
        /// <param name="isRun">是否启用内存缓存</param>
        public static void AddCache<T>(string key, T t, int minutes = 0)
        {
            key = GetKey(key);
            CacheManager.DefaultCache.Set<T>(key, t, minutes);
        }

        /// <summary>
        /// 移除Redis缓存
        /// </summary>
        /// <param name="key">key</param>
        public static void Remove(string key)
        {
            //RemoveRun(key);
            key = GetKey(key);
            CacheManager.DefaultCache.Remove(key);
        }

        #endregion

    }
}
